user credentials can now be shown, and can be removed, still to add them dynamically

Umar M. Sheikh 11 years ago
parent
commit
3fafbe02ef

+ 1 - 0
app/assets/javascripts/application.js.coffee.erb

@@ -6,6 +6,7 @@
6 6
 #= require jquery.json-editor
7 7
 #= require latlon_and_geo
8 8
 #= require ./worker-checker
9
+#= require ./users
9 10
 #= require_self
10 11
 
11 12
 window.setupJsonEditor = ($editor = $(".live-json-editor")) ->

+ 6 - 0
app/assets/javascripts/users.js

@@ -0,0 +1,6 @@
1
+//alert("i get included");
2
+function remove_fields(link){
3
+  $(link).prev().val("1");
4
+  alert($(link).prev().val())
5
+  $(link).parent(".fields").hide();
6
+}

+ 13 - 0
app/helpers/application_helper.rb

@@ -14,4 +14,17 @@ module ApplicationHelper
14 14
       link_to '<span class="label label-warning">No</span>'.html_safe, agent_path(agent, :tab => (agent.recent_error_logs? ? 'logs' : 'details'))
15 15
     end
16 16
   end
17
+
18
+  def link_to_remove_fields(name, f, options = {})
19
+    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)", options)
20
+  end
21
+
22
+  def link_to_add_fields(name, f, options = {})
23
+    new_object = f.object.class.reflect_on_association(association).klass.new
24
+    fields = f.fields_for(association, new_object, :child_index => "new_#{ association }") do |builder|
25
+      render(association.to_s.singularize + "_fields", :f => builder)
26
+    end
27
+    link_to_function(name, "add_fields(this, \"#{ association }\", \"#{ escape_javascript(fields) }\")", options)
28
+  end
29
+  
17 30
 end

+ 4 - 0
app/models/user.rb

@@ -22,6 +22,10 @@ class User < ActiveRecord::Base
22 22
   validates_format_of :username, :with => /\A[a-zA-Z0-9_-]{3,15}\Z/, :message => "can only contain letters, numbers, underscores, and dashes, and must be between 3 and 15 characters in length."
23 23
   validates_inclusion_of :invitation_code, :on => :create, :in => INVITATION_CODES, :message => "is not valid"
24 24
 
25
+  has_many :user_credentials, :dependent => :destroy
26
+  accepts_nested_attributes_for :user_credentials, :reject_if => lambda { |attrs| attrs.all? { |key, value| value.blank? } },
27
+                                :allow_destroy => true
28
+  attr_accessible :user_credentials_attributes
25 29
   has_many :events, :order => "events.created_at desc", :dependent => :delete_all, :inverse_of => :user
26 30
   has_many :agents, :order => "agents.created_at desc", :dependent => :destroy, :inverse_of => :user
27 31
   has_many :logs, :through => :agents, :class_name => "AgentLog"

+ 4 - 0
app/models/user_credential.rb

@@ -0,0 +1,4 @@
1
+class UserCredential < ActiveRecord::Base
2
+  attr_accessible :credential_name, :credential_value, :user_id
3
+  belongs_to :user
4
+end

+ 8 - 0
app/views/devise/registrations/_user_credentials_fields.html.erb

@@ -0,0 +1,8 @@
1
+<p class="fields">
2
+  <%= f.label :credential_name, "Name" %>
3
+  <%= f.text_field :credential_name %>
4
+  <%= f.label :credential_value, "Value" %>
5
+  <%= f.text_field :credential_value %>
6
+  <%= f.hidden_field :_destroy %>
7
+  <%= link_to_function "remove", "remove_fields(this)" %>
8
+</p>

+ 8 - 1
app/views/devise/registrations/edit.html.erb

@@ -1,3 +1,4 @@
1
+hello doctor
1 2
 <div class='container'>
2 3
   <div class='row'>
3 4
     <div class='span8 offset2'>
@@ -48,6 +49,12 @@
48 49
           <div class='form-actions'>
49 50
             <%= f.submit "Update", :class => "btn btn-primary" %>
50 51
           </div>
52
+          now the new stuff
53
+          <div class="control-group">
54
+            <%= f.fields_for(:user_credentials) do |uc| %>
55
+              <%= render 'user_credentials_fields', :f => uc %>
56
+            <% end %>
57
+          </div>
51 58
         <% end %>
52 59
 
53 60
         <h3>Cancel my account</h3>
@@ -58,4 +65,4 @@
58 65
       </div>
59 66
     </div>
60 67
   </div>
61
-</div>
68
+</div>

+ 12 - 0
db/migrate/20140121075418_create_user_credentials.rb

@@ -0,0 +1,12 @@
1
+class CreateUserCredentials < ActiveRecord::Migration
2
+  def change
3
+    create_table :user_credentials do |t|
4
+      t.integer :user_id
5
+      t.string :credential_name
6
+      t.string :credential_value
7
+
8
+      t.timestamps
9
+    end
10
+    add_index :user_credentials, [:user_id, :credential_name], :unique => true
11
+  end
12
+end

+ 5 - 0
spec/models/user_credential_spec.rb

@@ -0,0 +1,5 @@
1
+require 'spec_helper'
2
+
3
+describe UserCredential do
4
+  pending "add some examples to (or delete) #{__FILE__}"
5
+end